<?php
class RecommendAction extends Action{
   public function _initialize(){//初始化
		R("Home.Public","top");
		R("Home.Public","bottom");
   }
   public function index(){//推荐产品
	  import("ORG.Com.DiyCache");
	  $mycache = new DiyCache;
	  if(!$mycache->is_cached()){//处理自定义缓存
	     /*安全处理*/
		 checkget();
		 checkpost();
		 /*安全处理*/
		 
		 //首页四张切换(761*361) 
		 $flash = M("flash");
		 $typeid = 62;
		 $flash_index_four = $flash->where("typeid = $typeid")->order("sequence desc")->limit(4)->findAll();
		 $this->assign("flash_index_four",$flash_index_four);
		 
		 $product = M("product");
		 $sysclass = M("sysclass");
		 $col_id = getcol_id("product");
		 $typeid = $_POST["typeid"]?$_POST["typeid"]:$_GET["typeid"];
		 $map = array();
		 if(!empty($typeid)) {
			 if(!is_numeric($typeid))alert('参数错误');
			 $first_typeid = $typeid;
			 $map["typeid"] = array("in",typeid2child($typeid,"sysclass","col_id = {$col_id}"));
		 }else{
			 $typeid = 0;
			 $first_typeid = $sysclass->where("parid = 0 and col_id = $col_id")->order("sequence desc")->getField("id");
		 }
		 
		 //获取分类名称
		 $parid = $sysclass->where("id = $first_typeid")->getField("parid");
		 if(empty($parid)){
			 $parid = $first_typeid;
		 }
		 $this->assign("parid",$parid);
		 
		 //获取一级分类列表
		 $par_type_list = $sysclass->where("parid = 0 and col_id = $col_id")->order("sequence desc")->findAll();
		 
		 $this->assign("par_type_list",$par_type_list);
		 
		  //在结果中搜索
		 $small_keyword = $_POST["small_keyword"]?$_POST["small_keyword"]:base64_decode($_GET["small_keyword"]);
		 if(!empty($small_keyword)){
			 $map["pr_name"]=array("like","%$small_keyword%"); 
		 }else{
			 $small_keyword = 0;
		 }
		 
		 //加上推荐 条件
		 $map["pr_rank"]="推荐";
		 
		 $pr_count = $product->where($map)->count();
		 $this->assign("pr_count",$pr_count);
		 
		 Vendor("LJW.page");
		 $order = $_GET["order"]?$_GET["order"]:"new";  // new   price
		 
		 if($order == "new"){
			 $order_str = "upload_date desc"; 
		 }else if($order == "price_up"){
			 $order_str = "pr_shop_price asc";
		 }else if($order == "price_down"){
		     $order_str = "pr_shop_price desc";
		 }
		 $this->assign("order",$order);
		 
		 $seq_url = U("Recommend/index",array("typeid"=>$typeid,"small_keyword"=>urlencode(base64_encode($small_keyword))));
		 
		 $small_keyword_url = U("Recommend/index",array("typeid"=>$typeid,"order"=>$order));
		 
		 $div_url = $go_url = U("Recommend/index",array("typeid"=>$typeid,"order"=>$order,"small_keyword"=>urlencode(base64_encode($small_keyword))));
		 
		 $this->assign("seq_url",$seq_url);
		 $this->assign("small_keyword_url",$small_keyword_url);
		 $this->assign("go_url",$go_url);
		 
		 $mypage = new page($pr_count,20,3,$div_url);
		 
		 //分页
		  //上一页   下一页 
		 if($mypage->absolutepage <> 1) $prev_page="<span class='next_bz'><a href='".$mypage->url."/p/".($mypage->absolutepage-1)."'>上一页</a></span>";	
		 if($mypage->absolutepage <> $mypage->pagenum) $next_page="<span class='next_bz'><a href='".$mypage->url."/p/".($mypage->absolutepage+1)."'>下一页</a></span>";
		 
		 $start=$mypage->absolutepage-4;
		 if($start<1)$start=1;
		 $end=$start+8;
		 if($end>$mypage->pagenum) $end=$mypage->pagenum;
			 
		 $start_url=$mypage->url."/p/".$start;
		 $end_url=$mypage->url."/p/".$end;
			 
		 $f_url=$mypage->url."/p/1";//第一页
		 $l_url=$mypage->url."/p/".$mypage->pagenum;//最后一页
			 
		 if($start<>1)$f_str="<a href='$f_url'>1</a><a href='#'>…</a>";
		 if($end<>$mypage->pagenum)$l_str="<a href='#'>…</a><a href='$l_url'>".$mypage->pagenum."</a>";
			 
		 for($i=$start;$i<=$end;$i++){
			if($mypage->absolutepage == $i){
			   $m_str.="<a href='".$mypage->url."/p/".$i."' class='on_link' >$i</a>";
			}else{
			   $m_str.="<a href='".$mypage->url."/p/".$i."'>$i</a>";	
			}
			
		 }
		 $div_str.=$prev_page.$f_str.$m_str.$l_str.$next_page;
		 
		 if(!empty($pr_count))$this->assign("divpage",$div_str);
		
		 $pr_list = $product->where($map)->limit($mypage->start.",".$mypage->pagesize)->order($order_str)->findAll();
		 
		 
		 $this->assign("pr_list",$pr_list);
		 
		 //导航 名称
		 if(!empty($typeid)){
			 $column_name = $sysclass->where("id = $typeid")->getField("class_name");
		 }else{
			 $column_name = "全部"; 
		 }
		 $this->assign("column_name",$column_name);

		if(C("diy_cache"))$mycache->create_cache($this->fetch());//创建缓存 
	    $this->display();
	  }
   }
}